import { LayerAction, LayerActionTypes } from '../actions';
import { Layer } from '../../models';


export interface LayerState {
    entities: { [id: number]: Layer },
    lastId: number
}

export const initialLayerState: LayerState = {
    entities: {},
    lastId: 0
}

export function LayerReducer(state: LayerState = initialLayerState, action: LayerAction): LayerState {
    switch(action.type) {
        case LayerActionTypes.ADD_LAYER: {
            let id = state.lastId + 1;
            let layer = action.payload;
            return {
                entities: {
                    ...state.entities,
                    [id]: layer
                },
                lastId: id
            }
        }
        // case LayerActionTypes.REMOVE_LAYER: {
        //     const id = action.payload.id;
        //     let entities = state.entities;
        //     delete entities[id];
        //     return {
        //         ...state,
        //         entities
        //     }
        // }
        case LayerActionTypes.CLEAR_LAYER: {
            return initialLayerState;
        }
        default: {
            return state;
        }
    }
}

export const getLayerEntities = (state: LayerState) => state.entities;
export const getLayerLastId = (state: LayerState) => state.lastId;